Skip to content

Ensure pixels have __len__ before checking with len()#2

Open
patch0 wants to merge 1 commit intomasterfrom
adjust-pixel-length-checks
Open

Ensure pixels have __len__ before checking with len()#2
patch0 wants to merge 1 commit intomasterfrom
adjust-pixel-length-checks

Conversation

@patch0
Copy link

@patch0 patch0 commented Feb 27, 2026

In Astro Pi we were getting TypeErrors when checking len() on non-iterable variables, e.g. when a user calls set_pixel() with just three arguments.

This PR ensures that the pixel has a len attribute before testing its length.

Also fixes checking the pixel list in set_pixels.

In Astro Pi we were getting TypeErrors when checking len() on non-iterable variables, e.g. when a user calls set_pixel() with just three arguments.

This PR ensures that the pixel has a __len__ attribute before testing its length.

Also fixes checking the pixel list in set_pixels.
Copilot AI review requested due to automatic review settings February 27, 2026 08:55
patch0 added a commit to RaspberryPiFoundation/editor-ui that referenced this pull request Feb 27, 2026
…`sense_hat.py` errors (#1348)

Closes
RaspberryPiFoundation/digital-maintenance-team#7

This commit specifically fixes the issue triggered by using incorrect
arguments in the `set_pixel` and `set_pixels` functions.

`TypeError: object of type 'int' has no len() on line 1081 of
sense_hat.py`

I've added an explicit check to see if the pixel object has a `__len__`
attribute.

I've opened an identical PR on the [Sense Hat
library](RaspberryPiFoundation/sense_hat#1), and
[here](RaspberryPiFoundation/python-sense-hat#2)
and [here](astro-pi/python-sense-hat#151)!

## Traceback mangling

I changed the error message when the filename is `./sense_hat.py` to
point to the next file in the stack, hopefully the user's code.

The issue with this is that it effectively masks errors in the
`sense_hat` library, blaming the user instead. At the moment it only
catches specific errors: `ValueError`, `RuntimeError` which are
specifically mentioned in the sense hat shim.

## Package update

This also updates the package version to 0.34.7.

## Example python

```python
# Import the libraries
from sense_hat import SenseHat
from time import sleep

# Set up the Sense HAT
sense = SenseHat()

sense.set_pixel(1, 2, 3)

# Or:
sense.set_pixels(1)

# Or:
# sense.set_pixels([1]*64)
```

## Before

<img width="1473" height="347" alt="image"
src="https://github.com/user-attachments/assets/0f8f6e1d-42bc-454f-88b5-a7c22568c012"
/>

## After

Tested on
https://staging-editor-static.raspberrypi.org/branches/1348_merge/web-component.html

<img width="1200" height="390" alt="image"
src="https://github.com/user-attachments/assets/f30ff39c-0c47-4dbf-a7ae-d1aed3951259"
/>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves error handling in the Sense HAT library's pixel manipulation functions by adding hasattr checks before calling len(). This prevents TypeError exceptions when users pass non-iterable arguments (e.g., integers) to functions expecting pixel tuples or lists, replacing them with more informative ValueError exceptions.

Changes:

  • Added hasattr(pixel_list, '__len__') check in set_pixels() before validating pixel list length
  • Added hasattr(pix, '__len__') check in set_pixels() before validating individual pixel length
  • Refactored set_pixel() to consolidate pixel validation with hasattr check, removing early length check that could raise TypeError

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

jamiebenstead pushed a commit to RaspberryPiFoundation/editor-ui that referenced this pull request Mar 3, 2026
…`sense_hat.py` errors (#1348)

Closes
RaspberryPiFoundation/digital-maintenance-team#7

This commit specifically fixes the issue triggered by using incorrect
arguments in the `set_pixel` and `set_pixels` functions.

`TypeError: object of type 'int' has no len() on line 1081 of
sense_hat.py`

I've added an explicit check to see if the pixel object has a `__len__`
attribute.

I've opened an identical PR on the [Sense Hat
library](RaspberryPiFoundation/sense_hat#1), and
[here](RaspberryPiFoundation/python-sense-hat#2)
and [here](astro-pi/python-sense-hat#151)!

## Traceback mangling

I changed the error message when the filename is `./sense_hat.py` to
point to the next file in the stack, hopefully the user's code.

The issue with this is that it effectively masks errors in the
`sense_hat` library, blaming the user instead. At the moment it only
catches specific errors: `ValueError`, `RuntimeError` which are
specifically mentioned in the sense hat shim.

## Package update

This also updates the package version to 0.34.7.

## Example python

```python
# Import the libraries
from sense_hat import SenseHat
from time import sleep

# Set up the Sense HAT
sense = SenseHat()

sense.set_pixel(1, 2, 3)

# Or:
sense.set_pixels(1)

# Or:
# sense.set_pixels([1]*64)
```

## Before

<img width="1473" height="347" alt="image"
src="https://github.com/user-attachments/assets/0f8f6e1d-42bc-454f-88b5-a7c22568c012"
/>

## After

Tested on
https://staging-editor-static.raspberrypi.org/branches/1348_merge/web-component.html

<img width="1200" height="390" alt="image"
src="https://github.com/user-attachments/assets/f30ff39c-0c47-4dbf-a7ae-d1aed3951259"
/>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants